/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.manchester.biology.eqhei.implementations;

import edu.manchester.biology.eqhei.interfaces.eQHEI_AttributeSet;
import edu.manchester.biology.eqhei.interfaces.eQHEI_Section;
import java.util.ArrayList;

/**
 *
 * @author Michael Kendall
 */
public class eQHEI_SectionIMP implements eQHEI_Section{
    private ArrayList<eQHEI_AttributeSet> attributeSets = new ArrayList<>();
    private int attributeSetIndex = -1;
    private String name = "";
    
    public eQHEI_SectionIMP(){
        
    }
    
    public eQHEI_SectionIMP(String n){
        name = n;
    }

    @Override
    public eQHEI_AttributeSet next() throws Exception {
        eQHEI_AttributeSet rvalue;
        if(attributeSetIndex >= attributeSets.size()-1){
            attributeSetIndex = attributeSets.size();
            throw new Exception("AttributeSet index out of bounds on next");
        }
        ++attributeSetIndex;
        rvalue = attributeSets.get(attributeSetIndex);
        return rvalue;
    }

    @Override
    public eQHEI_AttributeSet previous() throws Exception {
        eQHEI_AttributeSet rvalue;
        if(attributeSetIndex == 0){
            --attributeSetIndex;
            throw new Exception("AttributeSet index out of bounds on previous");
        }
        --attributeSetIndex;
        rvalue = attributeSets.get(attributeSetIndex);
        return rvalue;
    }

    @Override
    public void add(eQHEI_AttributeSet attributeSet) {
        attributeSets.add(attributeSet);
    }

    @Override
    public eQHEI_AttributeSet get(String name) throws Exception {
        return attributeSets.get(find(name));
    }

    @Override
    public eQHEI_AttributeSet get(Integer i) throws Exception {
        return attributeSets.get(i);
    }

    @Override
    public Integer length() {
        return attributeSets.size();
    }

    @Override
    public String getName() {
        return this.name;
    }
    
    private int find(String name) throws Exception{
        eQHEI_AttributeSet a[] = new eQHEI_AttributeSet[attributeSets.size()];
        attributeSets.toArray(a);
        int i = 0;
        while(i<a.length && !a[i].getName().equalsIgnoreCase(name)){
            ++i;
        }
        if(i<a.length){
            return i;
        }
        else{
            throw new Exception(name + " Not Found");
        }
    }

    @Override
    public eQHEI_AttributeSet remove(String name) throws Exception {
        return attributeSets.remove(find(name));
    }
}
